package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.Company;
import com.banksoft.cps.bean.CompanyUser;
import com.banksoft.cps.bean.common.ErrorTip;
import com.banksoft.cps.bean.query.CompanyQuery;
import com.banksoft.cps.bean.query.CompanyUserQuery;
import com.banksoft.cps.bean.warpper.CompanyWarpper;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.service.CompanyService;
import com.banksoft.cps.util.PageUtils;
import com.banksoft.cps.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2017/12/22.
 */
@Controller
@RequestMapping("/admin/company/")
public class AdminCompanyController extends ControllerConstants{

    @Autowired
    CompanyService companyService;


    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(CompanyQuery query){
        List<CompanyWarpper> list =  companyService.list(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(CompanyQuery query){
        long count =  companyService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }
    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        CompanyWarpper company = companyService.findById(id);
        return toJSONString(createDataTip(company));
    }

    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(Company company, HttpServletRequest request){
        try {
            companyService.save(company);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }

    @RequestMapping(value = "listAll", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String listAll(){
        CompanyQuery query = new CompanyQuery();
        List<CompanyWarpper> list =  companyService.list(query);
        return toJSONString(createDataTip(list));
    }

    @RequestMapping(value = "audit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String audit(String companyId,String auditStatus,String auditResult, HttpServletRequest request){
        try {
            CompanyWarpper company =  companyService.findById(companyId);
            company.setAuditStatus(auditStatus);
            company.setAuditResult(auditResult);
            companyService.save(company);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "updatePassword", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updatePassword(String id,String password){
        try {
            Company old = companyService.findById(id);
            old.setPassword(password);
            companyService.save(old);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }

    @RequestMapping(value = "updateStatus", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateStatus(String id, String status){
        companyService.updateStatus(id, status);
        return toJSONString(SUCCESS);
    }

    //删除
    @RequestMapping(value = "delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteCompany(String ids){
        if(StringUtil.isNullOrEmpty(ids)){
            return toJSONString(new ErrorTip(1,"请选择要删除的记录"));
        }
        companyService.deleteByIds(ids.split(","));
        return toJSONString(SUCCESS);
    }
}
